package com.zf.shuashua.ui.mine;

import android.content.Intent;

import com.zf.shuashua.ui.repair.RepairActivity;
import com.zf.shuashua.utils.ToastUtil;
import com.vise.xsnow.http.callback.ACallback;
import com.zf.shuashua.ShuaApplication;
import com.zf.shuashua.api.HttpData;
import com.zf.shuashua.api.Store;
import com.zf.shuashua.bean.ListVideo;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class ListVideoFragmVM extends ViewModel {

    private MutableLiveData<ListVideo> refresh;
    private MutableLiveData<ListVideo> more;

    MutableLiveData<ListVideo> getRefresh() {
        if (refresh == null) {
            refresh = new MutableLiveData<>();
        }
        return refresh;
    }


    MutableLiveData<ListVideo> getMore() {
        if (more == null) {
            more = new MutableLiveData<>();
        }
        return more;
    }


    public void getDataRefresh(String user_id, int page) {
        HttpData.getInstance().getListVideo(user_id, page, new ACallback<ListVideo>() {


            public void onRealSuccess(ListVideo data) {
                if (data.isStatus()) {
                    getRefresh().setValue(data);
                }
            }

            @Override
            public void onSuccess(ListVideo data) {
                if (data.getCode() == 402 || data.getCode() == 406) {
                    ToastUtil.showToast(ShuaApplication.getContext(), "请重新登录");
                    Store.getInstance().logoutSiglent(ShuaApplication.getContext());
                    Intent intent = new Intent(ShuaApplication.getContext(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    ShuaApplication.getContext().startActivity(intent);
                } else if (data.getCode() == 401) {
                    Store.getInstance().logoutSiglent(ShuaApplication.getContext());
                } else if (data.getCode() == 407) {
                    Intent intent = new Intent(ShuaApplication.getContext(), RepairActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    ShuaApplication.getContext().startActivity(intent);
                } else if (data.getData() != null) {
                    onRealSuccess(data);
                } else {
//                    ToastUtil.showToast(ShuaApplication.getContext(),"error code is  "+data.getCode());
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                LogUtil.d(errMsg);

            }
        });
    }

    public void getDataMore(String user_id, int page) {
        HttpData.getInstance().getListVideo(user_id, page, new ACallback<ListVideo>() {


            void onRealSuccess(ListVideo data) {
                if (data.isStatus()) {
                    getMore().setValue(data);
                }
            }

            @Override
            public void onSuccess(ListVideo data) {
                if (data.getCode() == 402 || data.getCode() == 406) {
                    ToastUtil.showToast(ShuaApplication.getContext(), "请重新登录");
                    Store.getInstance().logoutSiglent(ShuaApplication.getContext());
                    Intent intent = new Intent(ShuaApplication.getContext(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    ShuaApplication.getContext().startActivity(intent);
                } else if (data.getCode() == 401) {
                    Store.getInstance().logoutSiglent(ShuaApplication.getContext());
                } else if (data.getCode() == 407) {
                    Intent intent = new Intent(ShuaApplication.getContext(), RepairActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    ShuaApplication.getContext().startActivity(intent);
                } else if (data.getData() != null) {
                    onRealSuccess(data);
                } else {
//                    ToastUtil.showToast(ShuaApplication.getContext(),"error code is  "+data.getCode());
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                LogUtil.d(errMsg);

            }
        });
    }


}